// 医生数据类型定义
export interface Doctor {
  socoimage: string
  doctorstate: string
  doctorname: string
  socoadmin: string
  doctordescword?: string
  doctorgood?: number
  doctorid?: number
  doctorprice?: number
  doctorposition?: string
  hospital?: string
}

// 模拟医生数据
export const mockDoctors: Doctor[] = [
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-female.png',
    doctorstate: '在线',
    doctorname: '涂晓瑜',
    socoadmin: '妇科',
    doctordescword: '擅长对妇产科常见病、多发病的治疗，特别是妇科内分泌疾患(多囊卵巢综合症、更年期综合症...)',
    doctorgood: 99,
    doctorid: 11942,
    doctorprice: 15,
    doctorposition: '主治医师',
    hospital: '东莞市第八人民医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-female.png',
    doctorstate: '三甲',
    doctorname: '王晓玲',
    socoadmin: '产科',
    doctordescword: '常规孕检，产前诊断与筛查。各种妇科疾病的治疗，计划生育手术。',
    doctorgood: 100,
    doctorid: 13665,
    doctorprice: 63,
    doctorposition: '副主任医师',
    hospital: '辽宁省妇幼保健院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-male.png',
    doctorstate: '在线',
    doctorname: '张医生',
    socoadmin: '心内科',
    doctordescword: '擅长冠心病、高血压、心律失常等心血管疾病的诊治',
    doctorgood: 98,
    doctorid: 15678,
    doctorprice: 50,
    doctorposition: '主任医师',
    hospital: '北京协和医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-female.png',
    doctorstate: '在线',
    doctorname: '李医生',
    socoadmin: '儿科',
    doctordescword: '擅长儿童常见病、多发病的诊治，儿童生长发育评估',
    doctorgood: 97,
    doctorid: 12345,
    doctorprice: 40,
    doctorposition: '副主任医师',
    hospital: '北京大学第一医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-male.png',
    doctorstate: '三甲',
    doctorname: '王医生',
    socoadmin: '皮肤科',
    doctordescword: '擅长皮肤美容、激光治疗、皮肤肿瘤的诊断和治疗',
    doctorgood: 96,
    doctorid: 9876,
    doctorprice: 80,
    doctorposition: '主治医师',
    hospital: '上海交通大学医学院附属瑞金医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-female.png',
    doctorstate: '在线',
    doctorname: '刘医生',
    socoadmin: '神经内科',
    doctordescword: '擅长脑血管病、癫痫、帕金森病等神经系统疾病的诊治',
    doctorgood: 95,
    doctorid: 8765,
    doctorprice: 60,
    doctorposition: '主任医师',
    hospital: '中山大学附属第一医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-male.png',
    doctorstate: '在线',
    doctorname: '陈医生',
    socoadmin: '骨科',
    doctordescword: '擅长骨折、关节疾病、脊柱疾病的诊治和手术治疗',
    doctorgood: 94,
    doctorid: 7654,
    doctorprice: 70,
    doctorposition: '副主任医师',
    hospital: '华中科技大学同济医学院附属协和医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-female.png',
    doctorstate: '三甲',
    doctorname: '赵医生',
    socoadmin: '呼吸内科',
    doctordescword: '擅长慢性阻塞性肺疾病、支气管哮喘、肺部感染等呼吸系统疾病',
    doctorgood: 93,
    doctorid: 6543,
    doctorprice: 45,
    doctorposition: '主治医师',
    hospital: '四川大学华西医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-female.png',
    doctorstate: '在线',
    doctorname: '林医生',
    socoadmin: '妇科',
    doctordescword: '擅长妇科肿瘤、宫颈疾病、妇科内分泌疾病的诊治',
    doctorgood: 98,
    doctorid: 5432,
    doctorprice: 55,
    doctorposition: '主任医师',
    hospital: '复旦大学附属妇产科医院'
  },
  {
    socoimage: 'https://img.icons8.com/color/96/000000/doctor-male.png',
    doctorstate: '在线',
    doctorname: '陈医生',
    socoadmin: '产科',
    doctordescword: '擅长高危妊娠、产前诊断、产科急危重症的救治',
    doctorgood: 97,
    doctorid: 4321,
    doctorprice: 65,
    doctorposition: '副主任医师',
    hospital: '北京大学第三医院'
  }
]

// 按科室筛选医生
export const filterDoctorsByDepartment = (doctors: Doctor[], department: string): Doctor[] => {
  // 安全检查：确保doctors是数组
  if (!Array.isArray(doctors)) {
    console.warn('⚠️ filterDoctorsByDepartment: doctors参数不是数组，返回空数组')
    return []
  }
  
  if (department === '全部') {
    return doctors
  }
  
  return doctors.filter(doctor => {
    if (!doctor || !doctor.socoadmin) return false
    
    // 处理特殊科室映射
    if (department === '妇产科') {
      return doctor.socoadmin === '妇科' || doctor.socoadmin === '产科'
    }
    if (department === '心血管内科') {
      return doctor.socoadmin === '心内科'
    }
    
    // 默认使用包含匹配
    return doctor.socoadmin.includes(department) || department.includes(doctor.socoadmin)
  })
}

// 按姓名搜索医生
export const searchDoctorsByName = (doctors: Doctor[], name: string): Doctor[] => {
  // 安全检查：确保doctors是数组
  if (!Array.isArray(doctors)) {
    console.warn('⚠️ searchDoctorsByName: doctors参数不是数组，返回空数组')
    return []
  }
  
  if (!name.trim()) {
    return doctors
  }
  return doctors.filter(doctor => 
    doctor && doctor.doctorname && doctor.doctorname.includes(name)
  )
}

// 按评分排序医生
export const sortDoctorsByRating = (doctors: Doctor[], order: 'asc' | 'desc' = 'desc'): Doctor[] => {
  return [...doctors].sort((a, b) => {
    const ratingA = a.doctorgood || 0
    const ratingB = b.doctorgood || 0
    return order === 'desc' ? ratingB - ratingA : ratingA - ratingB
  })
}

// 按价格排序医生
export const sortDoctorsByPrice = (doctors: Doctor[], order: 'asc' | 'desc' = 'asc'): Doctor[] => {
  return [...doctors].sort((a, b) => {
    const priceA = a.doctorprice || 0
    const priceB = b.doctorprice || 0
    return order === 'desc' ? priceB - priceA : priceA - priceB
  })
}

// 获取在线医生
export const getOnlineDoctors = (doctors: Doctor[]): Doctor[] => {
  return doctors.filter(doctor => doctor.doctorstate === '在线')
}

// 获取三甲医院医生
export const getGradeADoctors = (doctors: Doctor[]): Doctor[] => {
  return doctors.filter(doctor => doctor.doctorstate === '三甲')
}

// 分页获取医生列表
export const getPaginatedDoctors = (
  doctors: Doctor[], 
  page: number = 1, 
  pageSize: number = 10
): { doctors: Doctor[], total: number, page: number, pageSize: number } => {
  const startIndex = (page - 1) * pageSize
  const endIndex = startIndex + pageSize
  const paginatedDoctors = doctors.slice(startIndex, endIndex)
  
  return {
    doctors: paginatedDoctors,
    total: doctors.length,
    page,
    pageSize
  }
}

// 获取医生统计信息
export const getDoctorStats = (doctors: Doctor[]) => {
  const total = doctors.length
  const online = doctors.filter(d => d.doctorstate === '在线').length
  const gradeA = doctors.filter(d => d.doctorstate === '三甲').length
  const avgRating = doctors.reduce((sum, d) => sum + (d.doctorgood || 0), 0) / total
  const avgPrice = doctors.reduce((sum, d) => sum + (d.doctorprice || 0), 0) / total
  
  return {
    total,
    online,
    gradeA,
    avgRating: Math.round(avgRating * 10) / 10,
    avgPrice: Math.round(avgPrice)
  }
}

export default mockDoctors
